/*
Copyright 2010,2011 Matt Van Der Westhuizen

This file is part of CGLL.

CGLL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

CGLL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CGLL.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.sound;

import java.util.HashMap;

import java.util.Map;
import org.chaoticengine.cgll.entity.component.ActiveComponent;
import org.chaoticengine.cgll.entity.component.Command;
import org.chaoticengine.cgll.exceptions.LoadingException;
import org.chaoticengine.cgll.factory.SoundFactory;
import org.chaoticengine.cgll.random.RNG;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
 * Component that plays a sound on creation.
 *
 * @author Matt v.d. Westhuizen
 */
@Root
public class SoundEffectComponent extends ActiveComponent {
    private String soundFilename = "";
    private Sound sound = null;
    private boolean played = false;
    private float volume = 1.0f;
    private float pitch = 1.0f;
    private float pitchVariance = 0.0f;

    public SoundEffectComponent() {}

    @Override
    public Map<String, Command> getCommands() {
        return(new HashMap<String, Command>());
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        if ((!played) && (sound != null)) {
            // Find the screen center and calculate sound position
            Vector2f c = new Vector2f(-gc.getWidth() / 2.0f, -gc.getHeight() / 2.0f);
            c = c.add(getOwner().getPosition()).normalise();

            played = true;
            float pitchMod = pitchVariance * 0.5f - RNG.getInstance().nextFloat() * pitchVariance;
            sound.playAt(pitch + pitchMod, volume, c.x, c.y, 0.0f);
        }
    }

    @Element(name="sound")
    public String getSound() {
        return(soundFilename);
    }

    @Element(name="sound")
    public void setSound(String filename) {
        this.soundFilename = filename;

        try {
            this.sound = SoundFactory.getInstance().getSound(filename);
        } catch (LoadingException lEx) {
            System.out.println("Error: Failed to load sound effect '" + filename + "'.");
            lEx.printStackTrace();
            System.exit(-1);
        }
    }

    @Element(name="volume", required=false)
    public float getVolume() {
        return volume;
    }

    @Element(name="volume", required=false)
    public void setVolume(float volume) {
        this.volume = volume;
    }

    @Element(name="pitch", required=false)
    public float getPitch() {
        return pitch;
    }

    @Element(name="pitch", required=false)
    public void setPitch(float pitch) {
        this.pitch = pitch;
    }

    @Element(name="pitch_variance", required=false)
    public float getPitchVariance() {
        return pitchVariance;
    }

    @Element(name="pitch_variance", required=false)
    public void setPitchVariance(float pitchVariance) {
        this.pitchVariance = pitchVariance;
    }
}
